<template>
	<view class="contain">
		<view class="containArea">
			<!-- 头像 -->
			<view class="avatar">
				<view class="avatarText">
					头像
				</view>
				<view class="avatarRight" @click="changeAvatar">
					<image :src="userInfo.avatar" mode=""></image>
					<uni-icons type="right" size="20"></uni-icons>
				</view>
			</view>
			<!-- 名称 -->
			<view class="nickName">
				<view class="nickNameText">
					昵称
				</view>
				<input type="text" v-model="userInfo.nickname" />
			</view>
		</view>
		<view class="save" @click="changeName">
			保存
		</view>
	</view>
</template>

<script>
	import getImgUrl from '@/getImg/index.js'
	import {
		getUserInfo,
		changeName
	} from '@/api/user.js';
	export default {
		data() {
			return {
				userInfo:{
					avatar: "", //头像
					nickname: '', //名称
				}	
			};
		},
		onShow(){
			getUserInfo().then(res => {
				this.userInfo.avatar = getImgUrl(res.data.avatar)
				this.userInfo.nickname = res.data.nickname
			})
		},
		methods: {
			// 更换头像
			changeAvatar() {
				const that = this
				uni.chooseImage({
					count: 1,
					sourceType: ['album'], //从相册选择
					success: function(res) {
						let tempFile = res.tempFilePaths[0]
						uni.uploadFile({
							url: getImgUrl('api/front/user/info/userUpdatePhoto'),
							filePath: tempFile,
							name: 'multipart',
							header: {
								'Authori-zation': uni.getStorageSync('LOGIN_STATUS_TOKEN')
							},
							formData: {
								'model': 'product',
								'pid': 1
							},
							success:(res) => {
								let rebackData = JSON.parse(res.data)
								if(rebackData.code == 200){
									that.userInfo.avatar = getImgUrl(rebackData.data.url)
									uni.showToast({
										title: "更改头像成功",
										icon: "none"
									})
								}else{
									uni.showToast({
										title: "更改头像失败",
										icon: "none"
									})
								}
							},
							fail(err) {
								console.log('上传失败', err);
							}
						})
					},
					fail(err) {
						console.error('文件选择失败:', err)
					}
				})
			},
			// 更改名称
			changeName() {
				changeName({
					nickName: this.userInfo.nickname
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: "更改成功",
							icon: "none"
						})
					} else {
						uni.showToast({
							title: "更改失败",
							icon: "none"
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.contain {
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 30rpx;
		background: linear-gradient(to bottom, #ffe3be,80%, #fff4e6);

		// avatar & nickname
		.containArea {
			width: 96vw;
			height: 50vh;
			border-radius: 15rpx;
			background-color: #fff0df;
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-top: 10rpx;

			// avatar
			.avatar {
				width: 93%;
				height: 100rpx;
				display: flex;
				align-items: center;
				border-bottom: 2rpx solid gray;
				justify-content: space-between;

				.avatarText {
					font-weight: 600;
				}

				.avatarRight {
					width: 120rpx;
					height: 98rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					image {
						width: 80rpx;
						height: 80rpx;
						border-radius: 50%;
					}
				}
			}

			// nickName
			.nickName {
				width: 93%;
				height: 100rpx;
				display: flex;
				align-items: center;
				border-bottom: 2rpx solid gray;
				justify-content: space-between;

				.nickNameText {
					font-weight: 600;
				}

				input {
					width: 120rpx;
				}
			}
		}

		// save
		.save {
			width: 60vw;
			height: 70rpx;
			background-color: #ff9d2f;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 70rpx;
			border-radius: 15rpx;
		}
	}
</style>